import { createPageTemplateConfig, PageTemplate } from '@/components';
import useIntlCustom from '@/hooks/useIntlCustom';
import { message } from 'antd';
import { getPageConfig, type PositionRecord } from './pageConfig';

const PositionManagePage = () => {
  // 使用 useIntlCustom Hook
  const { translate } = useIntlCustom();

  // 在组件内部调用，传入 translate 函数
  const pageConfig = getPageConfig(translate);

  // 统一的操作处理函数
  const handleAction = (actionType: string, record: PositionRecord) => {
    console.log('Action triggered:', actionType, record);

    switch (actionType) {
      case 'view':
        message.info(`${translate('systemManage', 'detail')} ${record.positionName}`);
        break;
      case 'edit':
        message.info(`${translate('systemManage', 'edit')} ${record.positionName}`);
        break;
      case 'addChild':
        message.info(`${translate('systemManage', 'addChildPosition')} ${record.positionName}`);
        break;
      case 'delete':
        message.warning(`${translate('systemManage', 'delete')} ${record.positionName}`);
        break;
      default:
        message.error(`${translate('common', 'unknown')} ${actionType}`);
    }
  };

  // 使用配置构建器创建页面配置
  const templateConfig = createPageTemplateConfig<PositionRecord>()
    .setSearchConfig({
      fields: pageConfig.searchFields,
    })
    .setTableConfig({
      columns: pageConfig.columns,
      rowKey: 'positionId',
      props: {
        scroll: { x: 1000 },
        size: 'middle',
        expandable: {
          defaultExpandAllRows: false,
          expandRowByClick: false,
        },
      },
    })
    .setFormConfig({
      fields: pageConfig.formFields,
    })
    .setActionConfig({
      permissions: {
        create: true,
        update: true,
        delete: true,
        view: true,
      },
      onAction: handleAction,
    })
    .setCrudConfig(pageConfig.crudConfig)
    .build();

  return <PageTemplate<PositionRecord> {...templateConfig} />;
};

export default PositionManagePage;
